home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
APP
/
E-L
/
IQ_Test.cpt
/
IQ Test ƒ
/
IQ Test Sorce Code ƒ
/
DoTest.c
next >
Wrap
C/C++ Source or Header
|
1993-02-17
|
7KB
|
277 lines
#include "IQ Test.h"
#define CANCEL_ITEM -1
static GrafPtr sBombPictOffscreenPort,
sButtonPictOffscreenPort;
static pascal void DrawDlogPictProc(
DialogPtr theDialog,
short itemNumber );
static pascal Boolean FilterProc(
DialogPtr theDialog,
EventRecord *theEvent,
short *itemHit );
static GrafPtr MakeOffScreenPort(
const Rect *portRectPtr );
void DoTest()
{
short itemHit,
itemType;
Boolean dialogDone = FALSE;
DialogPtr theDialog;
Handle itemHandle;
Rect itemRect;
PicHandle bombPict,
buttonPict;
theDialog = GetNewDialog( BOMB_DLOG_ID, NIL_PTR, MOVE_TO_FRONT );
CenterAsAlert( theDialog );
/*
Set up the offscreen port containing the dialog bomb picture.
*/
GetDItem( theDialog, BOMB_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
sBombPictOffscreenPort = MakeOffScreenPort( &itemRect );
if ( sBombPictOffscreenPort == NIL_PTR )
return;
bombPict = GetPicture( BOMB_PICT_ID );
SetPort( sBombPictOffscreenPort );
DrawPicture( bombPict, &itemRect );
ReleaseResource( ( Handle ) bombPict );
/*
Set up the offscreen port containing the button picture.
*/
GetDItem( theDialog, BUTTON_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
sButtonPictOffscreenPort = MakeOffScreenPort( &itemRect );
if ( sButtonPictOffscreenPort == NIL_PTR )
return;
buttonPict = GetPicture( BUTTON_PICT_ID );
SetPort( sButtonPictOffscreenPort );
DrawPicture( buttonPict, &itemRect );
ReleaseResource( ( Handle ) buttonPict );
/*
Set up the drawing procedure for window updates.
*/
SetPort( theDialog );
GetDItem( theDialog, BOMB_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
SetDItem( theDialog, BOMB_PICT_USER_ITEM, itemType, ( Handle ) DrawDlogPictProc, &itemRect );
/*
Do the dialog.
*/
ShowWindow( theDialog );
while ( dialogDone == FALSE )
{
ModalDialog( FilterProc, &itemHit );
switch( itemHit )
{
case CANCEL_ITEM:
dialogDone = TRUE;
break;
}
}
/*
Clean up by disposing memory allocated in this routine.
*/
ClosePort( sBombPictOffscreenPort );
DisposePtr( ( *sBombPictOffscreenPort ).portBits.baseAddr );
DisposePtr( ( Ptr ) sBombPictOffscreenPort );
ClosePort( sButtonPictOffscreenPort );
DisposePtr( ( *sButtonPictOffscreenPort ).portBits.baseAddr );
DisposePtr( ( Ptr ) sButtonPictOffscreenPort );
DisposeDialog( theDialog );
}
static pascal Boolean FilterProc(
DialogPtr theDialog,
EventRecord *theEvent,
short *itemHit )
{
unsigned char charCode;
short itemHeight,
itemType,
itemWidth,
maxLeft,
maxTop,
newLeft,
newTop,
portHeight,
portWidth;
Handle itemHandle;
Point mouseLoc;
Rect bombRect,
itemRect,
newRect,
testRect;
switch ( ( *theEvent ).what )
{
case nullEvent:
/*
Check the mouse position. If it is near the button image, move the button image.
*/
mouseLoc = ( *theEvent ).where;
SetPort( theDialog );
GlobalToLocal( &mouseLoc );
GetDItem( theDialog, BUTTON_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
testRect = itemRect;
InsetRect( &testRect, -10, -10 );
if ( PtInRect( mouseLoc, &testRect ) )
{
do
{
GetDItem( theDialog, BOMB_PICT_USER_ITEM, &itemType, &itemHandle, &bombRect );
InsetRect( &bombRect, 3, 3 );
portWidth = bombRect.right - bombRect.left;
portHeight = bombRect.bottom - bombRect.top;
itemHeight = itemRect.bottom - itemRect.top;
itemWidth = itemRect.right - itemRect.left;
maxTop = portHeight - itemHeight;
maxLeft = portWidth - itemWidth;
do
newLeft = Random();
while ( newLeft < 0 );
newRect.left = bombRect.left + ( newLeft % maxLeft );
newRect.right = newRect.left + itemWidth;
do
newTop = Random();
while ( newTop < 0 );
newRect.top = bombRect.top + ( newTop % maxTop );
newRect.bottom = newRect.top + itemHeight;
testRect = newRect;
InsetRect( &testRect, -10, -10 );
}
while ( PtInRect( mouseLoc, &testRect ) );
GetDItem( theDialog, BUTTON_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
SetDItem( theDialog, BUTTON_PICT_USER_ITEM, itemType, itemHandle, &newRect );
DrawDlogPictProc( theDialog, BOMB_PICT_USER_ITEM );
}
*itemHit = 0;
return FALSE;
break;
case keyDown:
/*
A command-period signifies things are over.
*/
charCode = ( *theEvent ).message & charCodeMask;
if ( ( charCode == '.' ) && ( ( *theEvent ).modifiers & cmdKey ) )
{
*itemHit = CANCEL_ITEM;
return TRUE;
}
else
return FALSE;
break;
case mouseDown:
/*
Mimic a real system bomb by intercepting all mouseDown events to avoid beep sound
when clicking outside the dialog’s window.
itemHit is set to 0, a non-existent item.
*/
*itemHit = 0;
return TRUE;
break;
default:
return FALSE;
break;
}
}
static pascal void DrawDlogPictProc(
DialogPtr theDialog,
short itemNumber )
{
short itemType;
Handle itemHandle;
Rect itemRect;
/*
Update the drawing of the dialog by copying the images from the two offscreen
ports. The first offscreen port contains all of the dialog image except
the “button”; the second offscreen port contains the “button”. Two images
are necessary since the location of the “button” changes.
*/
GetDItem( theDialog, BOMB_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
CopyBits(
&( *sBombPictOffscreenPort ).portBits,
&( *theDialog ).portBits,
&( *sBombPictOffscreenPort ).portRect,
&itemRect,
srcCopy,
0L );
GetDItem( theDialog, BUTTON_PICT_USER_ITEM, &itemType, &itemHandle, &itemRect );
CopyBits(
&( *sButtonPictOffscreenPort ).portBits,
&( *theDialog ).portBits,
&( *sButtonPictOffscreenPort ).portRect,
&itemRect,
srcCopy,
0L );
}
GrafPtr MakeOffScreenPort(
const Rect *portRectPtr )
{
GrafPtr newOffscreenPort,
oldPort;
/*
Given a Rect which indicates the size and coordinates of the offscreen port,
create an offscreen port.
Return NIL_PTR if there was insufficient memory to create the offscreen port.
*/
GetPort( &oldPort );
newOffscreenPort = ( GrafPtr ) NewPtr( sizeof( GrafPort ) );
if ( newOffscreenPort != NIL_PTR )
{
OpenPort( newOffscreenPort );
( *newOffscreenPort ).portRect = *portRectPtr;
( *newOffscreenPort ).portBits.bounds = *portRectPtr;
RectRgn( ( *newOffscreenPort ).clipRgn, portRectPtr );
RectRgn( ( *newOffscreenPort ).visRgn, portRectPtr );
( *newOffscreenPort ).portBits.rowBytes =
( ( ( *portRectPtr ).right - ( *portRectPtr ).left + 15 ) >> 4 ) << 1;
( *newOffscreenPort ).portBits.baseAddr =
NewPtr( ( long ) ( *newOffscreenPort ).portBits.rowBytes *
( ( *portRectPtr ).bottom - ( *portRectPtr ).top ) );
if ( ( *newOffscreenPort ).portBits.baseAddr == NIL_PTR )
{
ClosePort( newOffscreenPort );
DisposPtr( ( Ptr ) newOffscreenPort );
newOffscreenPort = NIL_PTR;
}
else
{
SetPort( newOffscreenPort );
EraseRect( &( *newOffscreenPort ).portRect );
}
}
SetPort( oldPort );
return ( newOffscreenPort );
}